﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmRoleUpdate : Form
    {
        public int roleid = -1;
        public FrmRoleUpdate()
        {
            InitializeComponent();
            InitializePermissionPool();
            roleid = -1;
        }
        public FrmRoleUpdate(int id)
        {
            InitializeComponent();
            InitializePermissionPool();
            roleid = id;
        }
        private void FrmRoleUpdate_Load(object sender, EventArgs e)
        {
            if (roleid <= 0)
            {
                this.Text = "添加新的角色";
            }
            else
            {
                this.Text = "更新角色" + roleid;
                using (DataBase db = new DataBase())
                {
                    DataTable dt = db.ExecuteDataTable("select ROLE_NAME,PERMISSIONS FROM CJ_ROLE WHERE ID=" + roleid);
                    if (dt.Rows.Count == 1)
                    {
                        //TODO:load data
                        textBox_rolename.Text = dt.Rows[0][0].ToString();
                        string permissions = dt.Rows[0][1].ToString();
                        for (int i = 0; i < checkedListBox_permissions.Items.Count;i++ )
                        {
                            string value = ((CheckItem)checkedListBox_permissions.Items[i]).Value;
                            if (permissions.IndexOf(value) >= 0)
                            {
                                checkedListBox_permissions.SetItemChecked(i, true);
                            }

                        }
                    }
                }
            }

        }
        private void InitializePermissionPool()
        {
            //操作员管理opr,opc,opu,opd
            //客户管理customerr,customerc,customeru,customerd

            List<CheckItem> permissions = new List<CheckItem>();
            permissions.Add(new CheckItem { Text = "操作员管理", Value = "opmgr" });

            permissions.Add(new CheckItem { Text = "角色管理", Value = "rolemgr" });

            permissions.Add(new CheckItem { Text = "客户管理查看", Value = "customerr" });
            permissions.Add(new CheckItem { Text = "客户管理添加", Value = "customerc" });
            permissions.Add(new CheckItem { Text = "客户管理修改", Value = "customeru" });
            permissions.Add(new CheckItem { Text = "客户管理删除", Value = "customerd" });



            this.checkedListBox_permissions.DataSource = permissions;
            this.checkedListBox_permissions.DisplayMember = "Text";
            this.checkedListBox_permissions.ValueMember = "Value";
        
        }

        private void button_confirm_Click(object sender, EventArgs e)
        {
            string sql = "";
            string permissions = getPermissionPool();
            if (roleid <= 0)
            {
                //add new role
                sql = "INSERT INTO [CJ_ROLE]([ROLE_NAME],[PERMISSIONS]) VALUES (@ROLE_NAME,@PERMISSIONS)";
                using (DataBase db = new DataBase())
                {
                    db.AddParameter("ROLE_NAME", textBox_rolename.Text);
                    db.AddParameter("PERMISSIONS", permissions);
                    if (db.ExecuteNonQuery(sql)==1)
                    {
                        MessageBox.Show("新建角色成功！");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("新建角色失败！！！");
                    }
                }
            }
            else
            {
                //update current role
                sql = "UPDATE [CJ_ROLE] SET [ROLE_NAME] = @ROLE_NAME,[PERMISSIONS] = @PERMISSIONS WHERE ID=@ID";
                using (DataBase db = new DataBase())
                {
                    db.AddParameter("ROLE_NAME", textBox_rolename.Text);
                    db.AddParameter("PERMISSIONS", permissions);
                    db.AddParameter("ID", roleid);
                    if (db.ExecuteNonQuery(sql) == 1)
                    {
                        MessageBox.Show("更新角色成功！");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新角色失败！！！");
                    }
                }
            }
        }
        private string getPermissionPool()
        {
            string permissions = "";
            for (int i = 0; i < checkedListBox_permissions.Items.Count; i++)
            {
                if (checkedListBox_permissions.GetItemChecked(i))
                {
                    permissions += ((CheckItem)checkedListBox_permissions.Items[i]).Value+",";
                }
            }

            return permissions;
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
    public class CheckItem
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }
}
